package Dschude;

import common.Utils;

import java.util.HashMap;

import java.util.Locale;
import java.util.Map;

import java.util.ResourceBundle;

import javax.faces.context.FacesContext;

import javax.portlet.PortletConfig;
import javax.portlet.PortletRequest;

import oracle.adf.model.binding.DCIteratorBinding;

import oracle.jbo.Row;
import oracle.jbo.ViewCriteria;
import oracle.jbo.ViewCriteriaItem;
import oracle.jbo.ViewCriteriaRow;
import oracle.jbo.ViewObject;
import oracle.jbo.common.JboCompOper;

public class DsChudeBean {
    private String pageDsCauhoiUrl;
    private String langid;
    private String tieudedschude;

    public DsChudeBean() {
//        DCIteratorBinding iterHoidap =
//            Utils.getIteratorBinding("HoidapLinhvucView1Iterator");
//        iterHoidap.getViewObject().getViewCriteriaManager().clearViewCriterias();
//        iterHoidap.executeQuery();
    }

    public String getViewRender(){
        getLangid();
        getDSChude();
        return "";
    }
    
    private void getDSChude(){
        DCIteratorBinding iterHoidap =
            Utils.getIteratorBinding("HoidapLinhvucView1Iterator");
        ViewObject vo = iterHoidap.getViewObject();
        ViewCriteria vc = vo.createViewCriteria();
        vc.setName("HoidapViewCriteria");
        ViewCriteriaRow vcRow = vc.createViewCriteriaRow();
        ViewCriteriaItem vcItem = vcRow.ensureCriteriaItem("NgonNgu");
        vcItem.setOperator(JboCompOper.OPER_EQ);
        vcItem.setValue(langid);
        vc.addRow(vcRow);
        vo.getViewCriteriaManager().removeApplyViewCriteriaName("HoidapViewCriteria");
        vo.applyViewCriteria(vc);
        vo.executeQuery();
    }

    private String getResource(String resourceKey) {
        Locale locale = getPortalLocale();
        ResourceBundle bundle =
            ResourceBundle.getBundle("model.PortletsBundle", locale);
        return bundle.getString(resourceKey);
    }

    /**
     * Author       Date        Comment
     *  TớiNT    09/11/2012     Thêm mới
     * @return kiểu ngôn ngữ
     */
    private Locale getPortalLocale() {
        Locale portalLocale = new Locale("vi", "VN");
        String language = "";
        try {

            PortletRequest request =
                (PortletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();
            if (request.getParameter("ngonnguid") != null) {
                language = request.getParameter("ngonnguid");
                portalLocale = new Locale(language);
            }

        } catch (Exception e) {
            // TODO: Add catch code
            e.printStackTrace();
        }

        return portalLocale;
    }

    /**
     * Author       Date        Comment
     *  TớiNT    09/11/2012     Thêm mới
     * @return url danh sách câu hỏi
     */
    private String UrlDsCauHoi() {
        String url = "";
        try {
            PortletRequest request =
                (PortletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();
            if (request.getParameter("dsCauhoiUrl") != null)
                url = request.getParameter("dsCauhoiUrl").toString();
        } catch (Exception e) {
            // TODO: Add catch code
            e.printStackTrace();
        }

        return url;
    }

    private String Tieudechude() {
        String tieudechude = "Danh sách chủ đề";
        try {

            PortletRequest request =
                (PortletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();
            if (request.getParameter("tieudedschude") != null)
                tieudechude = request.getParameter("tieudedschude").toString();
        } catch (Exception e) {
            // TODO: Add catch code
            e.printStackTrace();
        }

        return tieudechude;
    }
    
    /**
     * Author       Date        Comment
     *  TớiNT    09/11/2012     Thêm mới
     * @return Lấy ra danh sách chủ đề được hiển thị, theo ngôn ngữ và theo ngày hết hạn
     */
//    public void dsChude() {
//        try {
//            DCIteratorBinding iterHoidap =
//                Utils.getIteratorBinding("HoidapLinhvucView1Iterator");
//            Row row = iterHoidap.getCurrentRow();
//            if (row.getAttribute("Hienthi").toString().equals("1")) {
//                Map<String, Object> mapPredicate =
//                    new HashMap<String, Object>();
//                mapPredicate.put("NgonNgu", getPortalLocale().getLanguage());
//                mapPredicate.put("NgayHetHan",
//                                 ">=" + Utils.getCurrentDate().dateValue().toString());
//                Utils.search("HoidapLinhvucView1Iterator", mapPredicate);
//            }
//        } catch (Exception e) {
//            // TODO: Add catch code
//            e.printStackTrace();
//        }
//    }

    public void setPageDsCauhoiUrl(String pageDsCauhoiUrl) {
        this.pageDsCauhoiUrl = pageDsCauhoiUrl;
    }

    public String getPageDsCauhoiUrl() {
        pageDsCauhoiUrl = UrlDsCauHoi();
        //dsChude();
        return pageDsCauhoiUrl;
    }

    public String getLangid() {
        langid = this.getPortalLocale().getLanguage().toString();
        return langid;
    }

    public void setTieudedschude(String tieudedschude) {
        this.tieudedschude = tieudedschude;
    }

    public String getTieudedschude() {
        tieudedschude = Tieudechude();
        return tieudedschude;
    }
}
